import java.io.File;
import java.util.Hashtable;
import java.util.Vector;

public class MailClient {
    
	
	public static Hashtable<String, Properties> deviceMap;
	public static Vector<String> confList = new Vector<String>();
	// hashtable <deviceID, deviceConfFilePath>
	// this variable needs to be edited by DeviceHandler threads to include more aliases defined by user.
	public static Hashtable<String, String> deviceList = new Hashtable<String, String>();
	
	
    public static void main(String[] args) {
        
		File[] inputFiles = null;  
	    File rootDirectory = new File("../conf");  
	    int numFiles = 0; // don't waste cpu if no new files
	    
	    // Let's poll the conf directory for new devices
	    // TODO: SEV1, Please handle cases where deviceID already exists. Append number to end of deviceID.
    	// TODO: SEV4, Please handle dynamic conf file changes.
        while (true) {  
  
            inputFiles = rootDirectory.listFiles();
            
            if (inputFiles != null && inputFiles.length > numFiles){  
            	
            	numFiles = inputFiles.length; 
            	
                for(int i = 0; i < inputFiles.length; i++){  
                	
                	String confFilePath = inputFiles[i].getAbsolutePath();
                	String confFileName = inputFiles[i].getName();
                	if (confFileName.endsWith(".conf")){
                		// since filesystems won't allow duplicate filename under same dir, assume devices are unique
                		String deviceID = confFileName.split(".conf")[0];
	                    deviceList.put(deviceID, confFilePath);  
	                    System.out.println("Found new device: " + confFileName);
	
	//                  String confFilePath="D:/Academics/U4/Capstone/workspace/conf";
	//                	String confFileName="thomas.conf.proc";
	//            		String confFileName = "jon.conf";
	                	
	                	// spawn a thread to poll variable "devices" for new eligible device. 
	                	// This thread is responsible for polling email account.
	                	new DeviceHandler(rootDirectory.getAbsolutePath(), confFileName);
                	}
                    
                }  
            }  
  
            try {  
                Thread.sleep(5000);  
            }  
            catch (InterruptedException ex){  
                // log  
            }  
        }  
 
    }
    
}